package br.inf.mfpo.fra4and.exemplo;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import br.inf.mfpo.fra4and.R;
import br.inf.mfpo.fra4and.activities.Fra4AndDetailsListActivity;
import br.inf.mfpo.fra4and.exemplo.adapters.CoisasCursorAdapter;
import br.inf.mfpo.fra4and.exemplo.columns.CoisasColumns;
import br.inf.mfpo.fra4and.exemplo.columns.PessoasColumns;

public class PessoasViewActivity extends Fra4AndDetailsListActivity<CoisasCursorAdapter> {
	private static final int LIST_LAYOUT = android.R.layout.simple_list_item_2;
	private static final String[] LIST_FROM = new String[] { CoisasColumns.NOME, CoisasColumns.DATA_AQUISICAO };
	private static final int[] LIST_TO = new int[] { android.R.id.text1, android.R.id.text2 };

	TextView sexo;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected CoisasCursorAdapter buildListAdapter() {
		return new CoisasCursorAdapter(this, LIST_LAYOUT, null, LIST_FROM, LIST_TO);
	}

	@Override
	protected String getContextMenuTitle(Cursor cursor) {
		return cursor.getString(1);
	}

	@Override
	protected View getDetailsLayout() {
		View details = View.inflate(this, R.layout.example_pessoas_view, null);
		sexo = (TextView) details.findViewById(R.id.textView4);
		return details;
	}

	@Override
	protected String getDetailsTitle() {
		return getString(R.string.pessoa);
	}

	@Override
	protected String getListTitle() {
		return getString(R.string.coisas);
	}

	@Override
	protected String[] getDetailsProjection() {
		return new String[] { PessoasColumns._ID, PessoasColumns.NOME, PessoasColumns.SEXO };
	}

	@Override
	protected String[] getListProjection() {
		return new String[] { CoisasColumns._ID, CoisasColumns.NOME, CoisasColumns.DATA_AQUISICAO };
	}

	@Override
	protected String getListSelection() {
		return CoisasColumns.PESSOAS_ID + " = ?";
	}

	@Override
	protected String[] getListArguments() {
		final String argument = getIntent().getData().getPathSegments().get(1);
		return new String[] { argument };
	}

	@Override
	protected Uri getListUri() {
		return CoisasColumns.CONTENT_URI;
	}

	@Override
	protected boolean canDelete() {
		return true;
	}

	@Override
	protected String getDeleteMessage(int position, Uri selection) {
		Cursor c = (Cursor) getList().getAdapter().getItem(position);

		if (c == null) {
			return "";
		}

		String name = c.getString(1);

		return getString(R.string.apagar, name);
	}

	@Override
	protected String getDeletedMessage() {
		return getString(R.string.apaguei);
	}

	@Override
	protected String getDeleteErrorMessage() {
		return "";
	}

	@Override
	protected void populateFields(Cursor cursor) {
		setHeaderValue(cursor.getString(1));
		String[] sexes = getResources().getStringArray(R.array.genero);
		sexo.setText(sexes[cursor.getInt(2)]);
	}

	@Override
	protected void restoreState(Bundle savedInstanceState) {
	}

	@Override
	protected void saveState(Bundle outState) {
	}

	@Override
	protected String getMoveDialogTitle() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void onMoveClickDialog(Uri selection, long newId, int position) {
		// TODO Auto-generated method stub
		
	}
}
